<template>
  <div class="search-page">
    <div class="container">
      <div class="page-header">
        <h1 class="page-title">搜索结果</h1>
      </div>
      
      <div class="search-section">
        <div class="search-form">
          <input 
            v-model="searchQuery" 
            type="text" 
            placeholder="输入关键词搜索文章..." 
            class="search-input"
            @keyup.enter="performSearch"
          >
          <button @click="performSearch" class="search-button">搜索</button>
        </div>
        
        <div v-if="searchQuery" class="search-info">
            <p v-text="'搜索 &quot;' + searchQuery + '&quot; 找到 ' + filteredPosts.length + ' 篇文章'"></p>
        </div>
      </div>
      
      <div class="posts-grid">
        <div 
          v-for="post in filteredPosts" 
          :key="post.id" 
          class="post-card"
        >
          <div class="post-image" v-if="post.image">
            <img :src="post.image" :alt="post.title">
          </div>
          <div class="post-content">
            <div class="post-meta">
              <span class="post-date" v-text="formatDate(post.date)"></span>
            </div>
            <h3 class="post-title">
              <router-link :to="`/post/${post.id}`" v-text="post.title"></router-link>
            </h3>
            <p class="post-excerpt" v-text="post.excerpt"></p>
            <div class="post-tags">
              <span 
                v-for="tag in post.tags" 
                :key="tag" 
                class="tag"
                v-text="tag"
              >
              </span>
            </div>
          </div>
        </div>
      </div>
      
      <div v-if="searchQuery && filteredPosts.length === 0" class="no-results">
        <p v-text="'没有找到与 \'' + searchQuery + '\' 相关的文章。'"></p>
        <p>请尝试其他关键词。</p>
      </div>
    </div>
  </div>
</template>

<script>
import { allPosts } from '../utils/mock.js'

export default {
  name: 'SearchPage',
  props: {
    query: {
      type: String,
      default: ''
    }
  },
  data() {
    return {
      searchQuery: this.query || '',
      posts: allPosts
    }
  },
  computed: {
    filteredPosts() {
      if (!this.searchQuery) {
        return []
      }
      
      const query = this.searchQuery.toLowerCase()
      return this.posts.filter(post => 
        post.title.toLowerCase().includes(query) ||
        post.excerpt.toLowerCase().includes(query) ||
        post.tags.some(tag => tag.toLowerCase().includes(query))
      )
    }
  },
  methods: {
    formatDate(dateString) {
      const options = { year: 'numeric', month: 'long', day: 'numeric' }
      return new Date(dateString).toLocaleDateString('zh-CN', options)
    },
    performSearch() {
      // 搜索功能已通过计算属性实现
      // 这里可以添加额外的搜索逻辑或分析
    }
  }
}
</script>

<style scoped>
.search-page {
  width: 100%;
  padding: 2rem 0;
}

.container {
  max-width: 1200px;
  margin: 0 auto;
  padding: 0 20px;
}

.page-header {
  margin-bottom: 2rem;
  padding-bottom: 1rem;
  border-bottom: 1px solid #eee;
}

.page-title {
  font-size: 2rem;
  color: #333;
  margin: 0;
}

.search-section {
  margin-bottom: 2rem;
}

.search-form {
  display: flex;
  gap: 1rem;
  margin-bottom: 1rem;
}

.search-input {
  flex: 1;
  padding: 0.8rem 1rem;
  border: 2px solid #ddd;
  border-radius: 30px;
  font-size: 1rem;
  transition: border-color 0.3s;
}

.search-input:focus {
  outline: none;
  border-color: #42b983;
}

.search-button {
  padding: 0.8rem 1.5rem;
  background-color: #42b983;
  color: white;
  border: none;
  border-radius: 30px;
  cursor: pointer;
  font-size: 1rem;
  transition: background-color 0.3s;
}

.search-button:hover {
  background-color: #359c6d;
}

.search-info {
  color: #666;
  font-size: 0.9rem;
}

.posts-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(350px, 1fr));
  gap: 2rem;
}

.post-card {
  background: white;
  border-radius: 10px;
  overflow: hidden;
  box-shadow: 0 5px 15px rgba(0, 0, 0, 0.1);
  transition: transform 0.3s, box-shadow 0.3s;
}

.post-card:hover {
  transform: translateY(-5px);
  box-shadow: 0 10px 25px rgba(0, 0, 0, 0.15);
}

.post-image img {
  width: 100%;
  height: 200px;
  object-fit: cover;
}

.post-content {
  padding: 1.5rem;
}

.post-meta {
  display: flex;
  justify-content: space-between;
  margin-bottom: 1rem;
  font-size: 0.9rem;
  color: #666;
}

.post-title {
  margin-bottom: 1rem;
}

.post-title a {
  text-decoration: none;
  color: #333;
  font-size: 1.3rem;
  font-weight: 600;
  transition: color 0.3s;
}

.post-title a:hover {
  color: #42b983;
}

.post-excerpt {
  color: #666;
  line-height: 1.6;
  margin-bottom: 1rem;
}

.post-tags {
  display: flex;
  flex-wrap: wrap;
  gap: 0.5rem;
}

.tag {
  background-color: #f0f0f0;
  color: #666;
  padding: 0.3rem 0.6rem;
  border-radius: 20px;
  font-size: 0.8rem;
}

.no-results {
  text-align: center;
  padding: 3rem;
  color: #666;
}

.no-results p {
  margin: 0.5rem 0;
}

@media (max-width: 768px) {
  .posts-grid {
    grid-template-columns: 1fr;
  }
  
  .search-form {
    flex-direction: column;
  }
  
  .page-title {
    font-size: 1.5rem;
  }
}
</style>